#!/usr/bin/env python

import requests
import json
import pprint
from tenacity import retry, retry_if_result, stop_after_attempt

libs = ["aac", "android-clat", "android-mock", "androidplot", "ant-glob", "antlr", "apache-commons-math", "apache-harmony", "apache-http", "apache-xml", "archive-patcher", "autotest", "avahi", "bison", "blktrace", "boringssl", "bouncycastle", "bsdiff", "bzip2", "caliper",
"c-ares", "cblas", "ceres-solver", "chromium-libpac", "chromium-trace", "chromium-webview", "clang", "cmockery", "compiler-rt", "conscrypt", "crcalc", "cros", "curl", "dagger2", "dbus", "dbus-binding-generator", "deqp", "dexmaker", "dhcpcd-6.8.2", "dlmalloc", "dng_sdk",
"dnsmasq", "doclava", "donuts", "drm_gralloc", "drm_hwcomposer", "droiddriver", "e2fsprogs", "easymock", "eclipse-basebuilder", "eclipse-windowbuilder", "eigen", "elfutils", "embunit", "emma", "esd", "expat", "eyes-free", "f2fs-tools", "fdlibm", "fec", "fio", "flac",
"fonttools", "freetype", "fsck_msdos", "gemmlowp", "giflib", "glide", "gmock", "google-benchmark", "google-breakpad", "google-fonts", "google-tv-pairing-protocol", "gptfdisk", "gtest", "guava", "guice", "hamcrest", "harfbuzz_ng", "hyphenation-patterns", "icu",
"ImageMagick", "ims", "iproute2", "ipsec-tools", "iptables", "iputils", "iw", "jacoco", "jarjar", "javasqlite", "javassist", "jcommander", "jdiff", "jemalloc", "jetty", "jhead", "jmdns", "jmonkeyengine", "jsilver", "jsmn", "jsoncpp", "jsr305", "jsr330", "junit",
"kernel-headers", "ksoap2", "libavc", "libbrillo", "libcap", "libcap-ng", "libchrome", "libcxx", "libcxxabi", "libdaemon", "libdivsufsort", "libdrm", "libedit", "libevent", "libexif", "libgdx", "libgsm", "libhevc", "libjpeg-turbo", "liblzf", "libmicrohttpd", "libmpeg2",
"libmtp", "libnfc-nci", "libnfc-nxp", "libnl", "libogg", "libopus", "libpcap", "libphonenumber", "libpng", "libselinux", "libunwind", "libunwind_llvm", "libusb", "libusb-compat", "libutf", "libvncserver", "libvorbis", "libvpx", "libvterm", "libweave", "libxml2",
"libyuv", "littlemock", "lld", "lldb", "llvm", "ltrace", "lz4", "lzma", "marisa-trie", "markdown", "mdnsresponder", "mesa3d", "messageformat", "minijail", "mksh", "mmc-utils", "mockftpserver", "mockito", "mockwebserver", "modp_b64", "mp4parser", "mtpd", "nanohttpd",
"nanopb-c", "naver-fonts", "netcat", "netperf", "neven", "nfacct", "nist-pkits", "nist-sip", "noto-fonts", "oauth", "objenesis", "okhttp", "opencv", "opencv3", "openfst", "openssh", "owasp", "parameter-framework", "pcre", "pdfium", "piex", "ppp", "proguard", "protobuf",
"regex-re2", "replicaisland", "rmi4utils", "robolectric", "roboto-fonts", "rootdev", "safe-iop", "scrypt", "selinux", "sfntly", "shflags", "skia", "sl4a", "slf4j", "smali", "snakeyaml", "sonic", "sonivox", "speex", "sqlite", "squashfs-tools", "srtp", "strace", "svox",
"tagsoup", "tcpdump", "testng", "timezonepicker-support", "tinyalsa", "tinycompress", "tinyxml", "tinyxml2", "tlsdate", "toybox", "tpm2", "tremolo", "unicode", "universal-tween-engine", "v8", "valgrind", "vboot_reference", "vixl", "vogar", "vulkan-validation-layers",
"webp", "webrtc", "wpa_supplicant_8", "xmlrpcpp", "xmlwriter", "xmp_toolkit", "zlib", "zopfli", "zxing"
]

def debug(x):
    # pprint.pprint(x)
    pass

@retry(retry=retry_if_result(lambda x: not x), stop=stop_after_attempt(3))
def search_in_github(target):
    headers= {
        "Authentication": "token TOKEN",
        "Accept": "application/vnd.github.mercy-preview+json"
    }
    url = "https://api.github.com/search/repositories?q=%s&sort=star"

    resp = requests.get(url % target, headers=headers)
    resp_obj = json.loads(resp.text)
    if "items" in resp_obj:
        items = resp_obj["items"]
        name = items[0]["name"]
        url = items[0]["clone_url"]
        debug(url)
        debug(name)
        debug(name == target)
        if name == target:
            return url
    return ""

for lib in libs:
    try:
        repo = search_in_github(lib)
        print lib,",",repo
    except:
        pass
# print search_in_github("autotest")